﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace wage.Forms
{
    public partial class TimeKeeperDataView : common.forms.baseForm
    {
        public TimeKeeperDataView()
        {
            InitializeComponent();
            fullMode = false;
            emNameEd.BackColor = common.Settings.myTheme.Info.BackColor;
            emNameEd.ForeColor = common.Settings.myTheme.Info.ForeColor;
        }
        public data.wageDS.wgWorkShiftRow selectedDataRow = null;
        protected bool fullMode
        {
            get
            {
                return dataGrid.Visible;
            }
            set
            {
                dataGrid.Visible = value;
                this.Height = this.dataGrid.Location.Y + 2 * SystemInformation.CaptionHeight +10;
                if (value) this.Height += dataGrid.Height;
            }
        }

        private void LoadData()
        {
            myWageDS.wgTimeKeep.Clear();

            common.DateRange drData = dateRange.GetDateRange();
            if (drData == null) return;
            if (emCodeChk.Checked && emCodeEd.Text.Trim() != "")
            {
                DataAccess.DataLibs.LoadData(myWageDS.wgTimeKeep, drData.FromDate, drData.ToDate, emCodeEd.Text, true);
                DataAccess.DataLibs.LoadData(myWageDS.wgTimeKeep, drData.FromDate, drData.ToDate, emCodeEd.Text, false);
            }
            else
                DataAccess.DataLibs.LoadData(myWageDS.wgTimeKeep, drData.FromDate, drData.ToDate);
            UpdateGridEmName();
        }
        private void DoFilter()
        {
            //string filter = "";
            //if (this.emCodeChk.Checked && this.emCodeEd.Text.Trim() != "")
            //    filter += (filter == "" ? "" : " AND ") + myWageDS.wgTimeKeep.emCodeColumn.ColumnName
            //        + " LIKE '" + this.emCodeEd.Text.Trim() + application.Consts.SQL_CMD_ALL_MARKER + "'";

            //if (this.workShiftCodeChk.Checked && this.workShiftCodeEd.Text.Trim() != "")
            //    filter += (filter == "" ? "" : " AND ") + myWageDS.wgTimeKeep.shiftCodeColumn.ColumnName
            //        + " LIKE '" + this.workShiftCodeEd.Text.Trim() + application.Consts.SQL_CMD_ALL_MARKER + "'";
            //timeKeepSource.Filter  = filter;
            //ShowReccount("[" + timeKeepSource.Count.ToString() + "]");
        }
        private void UpdateGridEmName()
        {
            for (int idx = 0; idx < myWageDS.wgTimeKeep.Count; idx++)
            {
                if (myWageDS.wgTimeKeep[idx].RowState == DataRowState.Deleted) continue;
                DataAccess.DataLibs.FindAndCache(myBaseDS.employee, myWageDS.wgTimeKeep[idx].emCode);
            }
        }
        private void findBtn_Click(object sender, EventArgs e)
        {
            try
            {
                fullMode = false;
                ClearNotifyError();
                if (dateRange.GetDateRange()==null)
                {
                    NotifyError(dateRangeLbl);
                    //common.SysLibs.ShowErrorMessage("Xin vui lòng chọn thời gian");
                    return;
                }
                LoadData();
                DoFilter();
                fullMode = true;
            }
            catch (Exception er)
            {
                this.ShowMessage(er.Message);
            }
        }

        private void closeBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            this.ShowMessage(e.Exception.Message);
        }

        private void emCodeChk_CheckedChanged(object sender, EventArgs e)
        {
            emCodeEd.Enabled = emCodeChk.Checked;
        }

        private void workShiftCodeChk_CheckedChanged(object sender, EventArgs e)
        {
            workShiftCodeEd.Enabled = workShiftCodeChk.Checked;
        }

        private void emCodeEd_Validated(object sender, EventArgs e)
        {
            try
            {
                data.baseDS.employeeRow row = null;
                if (emCodeEd.Text.Trim() != "")
                {
                    row = DataAccess.DataLibs.FindAndCache(myBaseDS.employee, emCodeEd.Text.Trim());
                }
                emNameEd.Text = (row == null ? "" : row.fullName);
            }
            catch (Exception er)
            {
                this.ErrorHandler(this,er);
            }
        }
    }
}
